package com.codeyang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codeyang.domain.UserCollection;
import com.codeyang.es.ProdEs;
import com.codeyang.feign.UserCollectSearchFeign;
import com.codeyang.mapper.UserCollectionMapper;
import com.codeyang.service.UserCollectionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 描述: TO-删除标志-DO
 *
 * @author CodeYang_Site
 * @version 2021/5/27 12:23
 */
@Slf4j
@Service
public class UserCollectionServiceImpl extends ServiceImpl<UserCollectionMapper, UserCollection> implements UserCollectionService {

    @Autowired
    private UserCollectionMapper userCollectionMapper;


    @Autowired
    private UserCollectSearchFeign userCollectSearchFeign;


    /**
     * 添加收藏|取消收藏
     *
     * @param userId
     * @param prodId
     */
    @Override
    public void addOrCancel(String userId, Long prodId) {
        log.info("添加或取消用户收藏，有用户id为{}，prodId为{}", userId, prodId);
        //1.根据 proid + userid 查询收藏对象是否已经在收藏列表了,如果在就是取消收藏
        UserCollection oldCollection = userCollectionMapper.selectOne(new LambdaQueryWrapper<UserCollection>()
                .eq(UserCollection::getUserId, userId)
                .eq(UserCollection::getProdId, prodId)
        );
        //2.不存在那么就是要添加进入收藏表
        if (ObjectUtils.isEmpty(oldCollection)) {
            //使用构造者模式创建对象
            UserCollection userCollection = UserCollection.builder().prodId(prodId).userId(userId).createTime(new Date()).build();
            userCollectionMapper.insert(userCollection);
            return;
        }

        // 是已经存在的就删除
        userCollectionMapper.deleteById(oldCollection.getId());
    }


    /**
     * 根据用户id分页查询用户收藏
     * step:
     * 1.需要查询用户的收藏表  prodId
     * 2.拿到prodIds 远程调用es  根据id查询
     * 3.组装对象返回
     * @param page
     * @param userId
     * @return
     */
    @Override
    public Page<ProdEs> findUserCollectPage(Page<UserCollection> page, String userId) {
        Page<ProdEs> prodEsPage = new Page<>(page.getCurrent(), page.getSize());
        //1.
        Page<UserCollection> userCollectionPage = userCollectionMapper.selectPage(page, new LambdaQueryWrapper<>());
        List<UserCollection> records = userCollectionPage.getRecords();
        //2.拿到ids
        List<Long> prodIds = records.stream().map(UserCollection::getProdId).collect(Collectors.toList());
        //3.远程调用es 根据ids 查询
        // 远程调用搜索服务
        List<ProdEs> prodEsList = userCollectSearchFeign.getProdEsByIds(prodIds);
        prodEsPage.setRecords(prodEsList);
        prodEsPage.setTotal(userCollectionPage.getTotal());
        return prodEsPage;
    }
}
